package weatherImporter;

import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JScrollPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import java.awt.GridLayout;
import javax.swing.JRadioButton;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

public class Window {
	public int selectedRatio;
	public String[] column_names = {"Day", "Month", "Year", "Hour", "Minute", "Temperature", "Humidity",
			"Dewpoint", "Barometer", "Windspeed", "Gustspeed", "Direction", "Rainlastmin", "Dailyrain",
			"Monthlyrain", "Yearlyrain", "Heatindex"};
	
	private JTable table;
	private JTable table_1;
	private JFrame frame;
	private JTextField textField;

	private JRadioButton rdbtnColumnNamesIn;
	private boolean printWithColNames;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Window window = new Window();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Window() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		//initializing variables
		Vector<String> colNames = new Vector<String>();
		for (String colname : column_names) {
			colNames.add(colname);
		}
		printWithColNames = true;
		final FlexModel fm1 = new FlexModel(colNames);
		final FlexModel fm2 = new FlexModel(colNames);
		
		frame = new JFrame("WeatherImporter");
		frame.setResizable(false);
		frame.setBounds(100, 100, 1182, 586);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(128, 0, 1038, 541);
		frame.getContentPane().add(panel_1);
		panel_1.setLayout(new GridLayout(2, 1, 0, 5));
		table = new JTable();
		JScrollPane scrollPane_1 = new JScrollPane();
		panel_1.add(scrollPane_1);
		scrollPane_1.setViewportView(table);
		
		table_1 = new JTable();
		table_1.setColumnSelectionAllowed(true);
		JScrollPane scrollPane_2 = new JScrollPane();
		panel_1.add(scrollPane_2);
		
		scrollPane_2.setViewportView(table_1);
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 130, 130, 170);
		frame.getContentPane().add(panel);
		
		JLabel lblSelectEvery = new JLabel("Select every");
		panel.add(lblSelectEvery);
		
		textField = new JTextField();
		panel.add(textField);
	
		textField.setText("10");
		textField.setColumns(10);
		
		JLabel lblRowFor = new JLabel(". row for output.");
		panel.add(lblRowFor);
		
		JButton btnSelect = new JButton("Select!");
		panel.add(btnSelect);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBounds(0, 0, 130, 130);
		frame.getContentPane().add(panel_2);
		
		JButton btnLoadFile = new JButton("Load CSV");
		panel_2.add(btnLoadFile);
		
		JButton btnTakeSelected = new JButton("Take Selected");
		panel_2.add(btnTakeSelected);
		
		JButton btnClearTable = new JButton("Clear Table");
		btnClearTable.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				fm2.clearTable();
			}
		});
		panel_2.add(btnClearTable);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBounds(0, 299, 130, 151);
		frame.getContentPane().add(panel_3);
		
		JButton btnWriteOutput = new JButton("Write Output");
		btnWriteOutput.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser fc = new JFileChooser();
				int retval = fc.showSaveDialog(frame);
				if (retval == JFileChooser.APPROVE_OPTION) {
					File save = fc.getSelectedFile();
					try {
						fm2.write(save, printWithColNames);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		
		JButton btnRemoveSelectedColumn = new JButton("Remove Column");
		btnRemoveSelectedColumn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				fm2.removeColumnAndData(table_1, table_1.getSelectedColumns());
			}
		});
		panel_3.add(btnRemoveSelectedColumn);
		panel_3.add(btnWriteOutput);
		
		rdbtnColumnNamesIn = new JRadioButton("Col. names in Output?");
		rdbtnColumnNamesIn.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent arg0) {
				printWithColNames = rdbtnColumnNamesIn.isSelected();
			}
		});
		rdbtnColumnNamesIn.setSelected(true);
		panel_3.add(rdbtnColumnNamesIn);
		btnTakeSelected.addActionListener(new ActionListener() {
			//Take selected button
			public void actionPerformed(ActionEvent arg0) {
				int[] selection = table.getSelectedRows();
				for (int i: selection) {
					fm2.addRow(fm1.getRow(i));
				}
				table_1.setModel(fm2);
			}
		});
		
		btnLoadFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				final JFileChooser fc = new JFileChooser();
				fc.setCurrentDirectory(new File("E:\\workspace\\WeatherImporter\\TestData"));
				int retval = fc.showOpenDialog(frame);
				if (retval == JFileChooser.APPROVE_OPTION) {
					try {
						fm1.loadFile(fc.getSelectedFile(), " ", true);
						table.setModel(fm1);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		btnSelect.addActionListener(new ActionListener() {
			//take every x-th row button
			public void actionPerformed(ActionEvent arg0) {
				for (int i=0; i<fm1.getRowCount(); i=i + Integer.parseInt(textField.getText())) {
					fm2.addRow(fm1.getRow(i));
				}
				table_1.setModel(fm2);
			}
		});
	}
	
}
